package com.xue.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xue.bean.User;
import com.xue.service.IUserService;
import com.xue.util.MD5Util;
import com.xue.util.UUIDUtil;
import com.xue.vo.LoginVo;

/**
 * 
 * @ClassName: LoginController 
 * @Description: 登录控制 controller
 * @author zzx
 *
 * @date 2020年1月6日
 */
@RestController
public class LoginController {

	@Autowired
	IUserService userService;
	
	/**
	 * 
	 * @Title: login  
	 * @Description: 登录校验  
	 * @param @return    参数  
	 * @return String    返回类型  
	 * @throws  
	 * @author zzx
	 * @date 2020年1月6日
	 */
	@RequestMapping("/getLogin")
	public String login(HttpServletResponse response, LoginVo loginVo) {
		//String userId = UUIDUtil.getId();
		String userId = loginVo.getUserId();
		String pasword = MD5Util.StringInMd5(loginVo.getPassword());
		loginVo.setPassword(pasword);
		String token = userService.login(response, loginVo);
		return "token : "+token;
	}
	
	@RequestMapping("/getToken")
	public String getToken(HttpServletResponse response , String token) {
		System.err.println("校验token");
		User user = userService.getByToken(response, token);
		if(user == null) {
			return "token过期，请重新登录";
		}
		return user.toString();
		
	}
	
	@RequestMapping("/setToken")
	public String setToken(HttpServletResponse response , String token) {
		System.err.println("校验刷新token");
		
		boolean bflag = userService.setToken(response, token);
		if(bflag) {
			return "token 刷新成功";
		}
		return "token 刷新失败";
		
	}
}
